package com.fanrenqiji.modules.business.dict.service.impl;


import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.service.BaseCommonService;
import com.fanrenqiji.modules.business.dict.dao.BusDictDao;
import com.fanrenqiji.modules.business.dict.entity.BusDict;
import com.fanrenqiji.modules.business.dict.service.BusDictService;
import com.fanrenqiji.modules.sys.entity.Dict;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年2月6日 下午2:26:29
 * @description 数据字典业务实现类
 */
@Service
@Transactional(readOnly = true)
public class BusDictCommonServiceImpl extends BaseCommonService implements BusDictService {

	@Autowired
	public BusDictDao busdictDao;

	/**
	 * 通过id获得字典值
	 */
	@Override
	@Cacheable(value = "busDict", key = "'BusDict-id-'+#id")
	public BusDict findOne(Long id) {
		return busdictDao.findOne(id);
	}

	/**
	 * 获得有效的字典分类
	 */
	@Override
	@Cacheable(value = "busDict", key = "'BusDict-findTypeList'")
	public List<String> findTypeList() {
		return busdictDao.findTypeList();
	}
	
	/**
	 * 获得有效的字典分类
	 */
	@Override
	@Cacheable(value = "busDict", key = "'BusDict-findAllList'")
	public List<BusDict> findAllList() {
		return busdictDao.findAllList();
	}

	/**
	 * 获得字典的分页 
	 */
	@Override
	public Page<BusDict> find(Page<BusDict> page, BusDict busDict) {
		DetachedCriteria dc = busdictDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(busDict.getType())) {
			dc.add(Restrictions.eq("type", busDict.getType()));
		}
		if (StringUtils.isNotEmpty(busDict.getDescription())) {
			dc.add(Restrictions.like("description", "%" + busDict.getDescription()+ "%"));
		}
		dc.add(Restrictions.eq(Dict.DEL_FLAG, Dict.DEL_FLAG_NORMAL));
		//根据 type sort 和id排序
		dc.addOrder(Order.asc("type")).addOrder(Order.asc("sort"))
				.addOrder(Order.desc("id"));
		return busdictDao.find(page, dc);
	}

	/**
	 * 保存或更新字典
	 */
	@Override
	@Transactional(readOnly = false)
	@CacheEvict(value = "busDict", allEntries = true)
	public void save(BusDict busDict) {
		busdictDao.save(busDict);
		/*BusDict tempBusDict = busdictDao.findBusDicsByTypeAndValue(busDict.getType(),busDict.getValue());
		@SuppressWarnings("unchecked")
		Map<String, List<BusDict>> dictMap = (Map<String, List<BusDict>>) CacheUtils
				.get(BusDictUtils.CACHE_BUS_DICT_MAP);
		if(dictMap!=null){
			List<BusDict> list = dictMap.get(tempBusDict.getType());
			//当前字典对象的类型在缓存中不存在,则将其加入缓存中
			if(null == list){
				 list = new ArrayList<BusDict>();
				list.add(tempBusDict);
			}else{  
				//当前字典对象的类型在缓存中存在,则更新缓存中字典为最新
				for(BusDict cacheBusDict: list){
					//如果当前字典对象在缓存中已经存在，则删除缓存中的数据，将当前字典对象加入缓存
					if(tempBusDict.getId().equals(cacheBusDict.getId())){
						list.remove(cacheBusDict);
						list.add(tempBusDict);
						break;
					}
				}
				
				//当前字典对象的类型在缓存中不存在，则将当前字典放入缓存 TODO 逻辑混乱
				List<Long> cacheValuelist = new ArrayList<Long>();
				for(BusDict cacheBusDict: list){
					cacheValuelist.add(cacheBusDict.getId());
				}
				if(!cacheValuelist.contains(tempBusDict.getId())){
					list.add(tempBusDict);
				}
			}
		}*/
	}
	
	@Override
	@CacheEvict(value = "busDict", allEntries = true)
	public void delete(Long id) {
		 busdictDao.deleteById(id);
	}

	@Override
	@Cacheable(value = "busDict", key = "'BusDict-'+#type")
	public List<BusDict> findBusDicsByType(String type) {
		return busdictDao.findBusDicsByType(type);
	}

	@Override
	@Cacheable(value = "busDict", key = "'BusDict-'+#type+'-'+#value")
	public BusDict findBusDicsByTypeAndValue(String type,String value) {
		return busdictDao.findBusDicsByTypeAndValue(type,value);
	}
	

}
